/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.action.product;

import com.yy.bugzillahelp.data.VersionData;
import com.yy.bugzillahelp.tool.SQLTool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hualun-alan
 */
public class VersionAction {

    private static final Logger logger = Logger.getLogger(VersionAction.class.getName());

    public static int insertOne(Connection con, VersionData versionData, String serverId) {
        String sql1 = "insert into versions(version_id,name,is_active,product_id,server_id)"
                + " values (?,?,?,?,?)";

        PreparedStatement ps1 = null;
        try {
            con.setAutoCommit(false);

            ps1 = con.prepareStatement(sql1);
            ps1.setString(1, versionData.getId());
            ps1.setString(2, versionData.getName());
            ps1.setString(3, versionData.isActive() ? "yes" : "no");
            ps1.setString(4, versionData.getProductId());
            ps1.setString(5, serverId);
            ps1.executeUpdate();

            con.commit();
            return 1;
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "sql1 = {0}", sql1);
            logger.log(Level.SEVERE, null, ex);
            return -1;
        } finally {
            SQLTool.closeStatement(ps1);
        }
    }

    public static int insertList(Connection con, ArrayList<VersionData> versionDatas, String serverId) {
        String sql1 = "insert into versions(version_id,name,is_active,product_id,server_id)"
                + " values (?,?,?,?,?)";

        PreparedStatement ps1 = null;
        try {
            con.setAutoCommit(false);

            ps1 = con.prepareStatement(sql1);
            ps1.setString(5, serverId);
            for (VersionData versionData : versionDatas) {
                ps1.setString(1, versionData.getId());
                ps1.setString(2, versionData.getName());
                ps1.setString(3, versionData.isActive() ? "yes" : "no");
                ps1.setString(4, versionData.getProductId());
                ps1.executeUpdate();
            }

            con.commit();
            return 1;
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "sql1 = {0}", sql1);
            logger.log(Level.SEVERE, null, ex);
            return -1;
        } finally {
            SQLTool.closeStatement(ps1);
        }
    }

    public static ArrayList<VersionData> searchVersions(Connection con, String productId, String serverId) {
        ArrayList<VersionData> versionDatas = new ArrayList<VersionData>();

        String sql1 = "select * from versions where product_id = ? and server_id = ? order by version_id ";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql1);
            ps.setString(1, productId);
            ps.setString(2, serverId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                VersionData versionData = new VersionData();
                versionData.setId(rs.getString("version_id"));
                versionData.setName(rs.getString("name"));
                versionData.setActive("yes".equalsIgnoreCase(rs.getString("is_active")));
                versionData.setProductId(rs.getString("product_id"));
                versionDatas.add(versionData);
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
        } finally {
            SQLTool.closeStatement(ps);
        }

        return versionDatas;
    }

    public static int delete(Connection con, String productId, String serverId) {
        return SQLTool.executeUpdate(con, "delete from versions where product_id = '" + productId + "' and server_id = '" + serverId + "'");
    }
}
